package cn.edu.nuc.film.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.nuc.film.common.PageControl;
import cn.edu.nuc.film.common.Pager;
import cn.edu.nuc.film.common.Parameter;
import cn.edu.nuc.film.common.UnitHelper;
import cn.edu.nuc.film.dao.CityFilmMapper;
import cn.edu.nuc.film.dao.FilmCityMapper;
import cn.edu.nuc.film.dao.FilmMapper;
import cn.edu.nuc.film.entity.CityFilm;
import cn.edu.nuc.film.entity.Film;
import cn.edu.nuc.film.entity.FilmCity;
import cn.edu.nuc.film.service.interfaces.FilmService;

@Service
public class FilmServiceImpl implements FilmService {
	
	@Autowired
	private FilmMapper filmMapper;
	@Autowired
	private FilmCityMapper fcMapper;
	@Autowired
	private CityFilmMapper cfMapper;
	
	

	/**
	 * 前台界面film列表
	 */
	public List<Film> indexFilmList() {
		List<Film> indexFilmList = filmMapper.indexFilmList();
		return indexFilmList;
	}

	
	
	/**
	 * 前台界面film的详情  
	 */
	public Film pageFilmDetail( int filmId){
		
		Film film = filmMapper.pageFilmDetail(filmId);
		return film;
		
	}
	
	
	
	/**
	 *  删除影片现属影城，重新添加影城，以实现影片影城的更新操作 
	 */
	public int deleteCityFilm(int filmId) {
		int count = cfMapper.deleteByPrimaryKey(filmId);
		return count;
	}
	
	/**
	 * 添加影城 ，一个list的添加
	 */
	public int saveFilmCity(int filmId, String[] fcids) {
		//声明、创建一个list对象
		List<CityFilm> list = new ArrayList<CityFilm>();
		//循环遍历，获取需要的CityFilm对象，并添加到list里，实现list的添加
		for( int i=0;i<fcids.length;i++){
			//声明、创建一个CityFilm对象
			CityFilm cf = new CityFilm();
			cf.setFilmId(filmId);
			cf.setFcId(Integer.parseInt(fcids[i]));
			//将CityFilm添加到list里
			list.add(cf);
		}
		System.out.println(list);
		int count = cfMapper.insertList(list);
		return count;
	}
	
	/**
	 * 获取影片影城权限
	 */
	public List<FilmCity> initFilmCity( Film film){
		return fcMapper.initFilmCity(film);
	}
	/**
	 * 查询所有的film
	 */
	public Pager<Film> filmList( PageControl pc,String name) {
		
		/**
		 * 设置dao层的参数列表( @Param("start") int start,
		 * @Param("offset") int offset,@Param("filmName") String filmName )
		 */
		Parameter para = new Parameter();
		para.setStart( (pc.getCurrentindex() - 1) * pc.getPagesize());
		para.setOffset( pc.getPagesize());
		para.setName(name);
		
		//获取每页的具体数据
		List<Film> list = filmMapper.filmList( para,0 );
		
		//设置总记录数
		int pagecount = 0;
		if( name != null ){
			pagecount = list.size();
		}else{
			pagecount = filmMapper.count();
		}
		/*System.out.println(pagecount);*/
		//测试是否获得
		/*System.out.println("-----------------filmlist------------------");
		System.out.println(list);*/
		Pager<Film> pager = UnitHelper.execlist(pagecount, list, pc);
		return pager;
	}
	
	/**
	 * 添加影片（film）
	 */
	public int insert(Film film) {
		
		int count = filmMapper.insert(film);
		return count;
	}
	
	/**
	 * 获取影片的信息，进行修改操作
	 */
	public Film detail(int filmId) {
		Film film = filmMapper.selectByFilmId(filmId);
		return film;
	}
	/**
	 * 修改影片信息
	 */
	public int updateByFilmId(Film film) {
		int count = filmMapper.updateByPrimaryKeySelective(film);
		if( count > 0){
			return count;
		}else{
			throw new RuntimeException("修改失败！");
		}
		
	}
	
	/**
	 * 删除影片，通过影片id进行删除
	 */
	public int deleteFilm(int filmId) {

		int count = filmMapper.deleteByPrimaryKey(filmId);
		if( count > 0){
			return count;
		}else{
			throw new RuntimeException("删除失败！");
		}
	}
	
	
	
}
